//
//  MLBleSetViewController.m
//  MLBleSDK
//
//  Created by 程杰 on 2023/10/20.
//

#import "MLBleSetViewController.h"

@interface MLBleSetViewController ()
@property (weak, nonatomic) IBOutlet UIView *bg1;
@property (weak, nonatomic) IBOutlet UIView *bg2;
@property (weak, nonatomic) IBOutlet UIView *bg3;
@property (weak, nonatomic) IBOutlet UIView *bg4;

@property (weak, nonatomic) IBOutlet UITextField *macTf;
@property (weak, nonatomic) IBOutlet UITextField *snTf;

@property (weak, nonatomic) IBOutlet UITextField *ydm;
@property (weak, nonatomic) IBOutlet UITextField *cmeiTf;

@property (weak, nonatomic) IBOutlet UITextField *changjiaTf;
@property (weak, nonatomic) IBOutlet UITextField *verTf;
@property (weak, nonatomic) IBOutlet UITextField *brandTf;
@property (weak, nonatomic) IBOutlet UITextField *powerTf;

@property (weak, nonatomic) IBOutlet UITextField *typeTf;

@property (weak, nonatomic) IBOutlet UITextField *pTokenTf;
@property (weak, nonatomic) IBOutlet UITextField *aTokenTf;

@end

@implementation MLBleSetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"参数设置";
    self.view.backgroundColor = [UIColor lightGrayColor];
    
    [self bgLayerForView:self.bg1];
    [self bgLayerForView:self.bg2];
    [self bgLayerForView:self.bg3];
    [self bgLayerForView:self.bg4];
    

    MLBleParamsItem *item = [MLBleParamsItem new];
    item.verType = @"v2.0";
    item.codeType = @"0d";// 获取 type ptoken atoken
    item.peripheral = self.model.peripheral;
    [[MLBLEManager sharedManager] setParamsInfo:item result:^(MLBleResultInfos * _Nonnull infos) {
        self.typeTf.text = infos.type;
        self.pTokenTf.text = infos.pToken;
        self.aTokenTf.text = infos.aToken;
        
        item.codeType = @"0e";// 获取 cmei sn 日期
        [[MLBLEManager sharedManager] setParamsInfo:item result:^(MLBleResultInfos * _Nonnull infos) {
            self.cmeiTf.text = infos.cmei;
            self.snTf.text = infos.sn;
            self.ydm.text = infos.ymd;
            
            item.codeType = @"0f";// 获取厂家 品牌  型号 供电
            [[MLBLEManager sharedManager] setParamsInfo:item result:^(MLBleResultInfos * _Nonnull infos) {
                self.changjiaTf.text = infos.manufacturer;
                self.brandTf.text = infos.brand;
                self.verTf.text = infos.version;
                self.powerTf.text = infos.powerup;
                
                item.codeType = @"23"; //获取 Mac
                [[MLBLEManager sharedManager] setParamsInfo:item result:^(MLBleResultInfos * _Nonnull infos) {
                    self.macTf.text = infos.mac;
                }];
                
            }];
        }];
    }];
    
    
}

- (IBAction)btnCkick:(id)sender {
    
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"请选择设置数据" message:@"" preferredStyle:UIAlertControllerStyleActionSheet];
      
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
       
    }];
    MLBleParamsItem *item = [MLBleParamsItem new];
    item.peripheral = self.model.peripheral;
    UIAlertAction *TOKEN = [UIAlertAction actionWithTitle:@"TOKEN" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        
        item.codeType = @"20";// 设置 type ptoken atoken
        item.type = self.typeTf.text;
        item.pToken = self.pTokenTf.text;
        item.aToken = self.aTokenTf.text;
        [[MLBLEManager sharedManager] setParamsInfo:item result:^(MLBleResultInfos * _Nonnull infos) {
            [SVProgressHUD showSuccessWithStatus:infos.message];
        }];
    }];
    
    UIAlertAction *CMEI = [UIAlertAction actionWithTitle:@"SN/CMEI" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        
        
        item.codeType = @"21";// 设置 cmei sn 日期
        item.cmei = self.cmeiTf.text;
        item.sn = self.snTf.text;
        item.ymd = self.ydm.text;
        [[MLBLEManager sharedManager] setParamsInfo:item result:^(MLBleResultInfos * _Nonnull infos) {
            [SVProgressHUD showSuccessWithStatus:infos.message];
        }];
        
    }];

    
    
    UIAlertAction *info = [UIAlertAction actionWithTitle:@"厂家信息" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        item.codeType = @"22";//设置 厂家 品牌 型号 供电
        item.manufacturer = self.changjiaTf.text;
        item.brand = self.brandTf.text;
        item.version = self.verTf.text;
        item.powerup = self.powerTf.text;
        [[MLBLEManager sharedManager] setParamsInfo:item result:^(MLBleResultInfos * _Nonnull infos) {
            [SVProgressHUD showSuccessWithStatus:infos.message];
        }];
        
    }];
    
    UIAlertAction *mac = [UIAlertAction actionWithTitle:@"MAC地址" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        item.codeType = @"24";//设置 mac
        item.mac = self.macTf.text;
        [[MLBLEManager sharedManager] setParamsInfo:item result:^(MLBleResultInfos * _Nonnull infos) {
            [SVProgressHUD showSuccessWithStatus:infos.message];
        }];
    }];
    
      
    [alertController addAction:cancelAction];
    [alertController addAction:TOKEN];
    [alertController addAction:CMEI];
    [alertController addAction:info];
    [alertController addAction:mac];
      
    [self presentViewController:alertController animated:YES completion:nil];
    
}

- (void)bgLayerForView:(UIView *)view{
    view.layer.borderWidth =1;
    view.layer.masksToBounds = YES;
    view.layer.cornerRadius = 15;
    view.layer.borderColor = [UIColor blackColor].CGColor;
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
